<?php

class users {
    //put your code here
  public function __construct($connection) {
    $this->Connection = $connection;
  }
  
  public $Connection;
  
  public function GetUserTable()
  {
    $queries = new mysqlqueries();
    
    $userInformation=$this->Connection->query($queries->GetTable($queries->User, "name"));
    
    $userList = array();
    while ($row = mysqli_fetch_array($userInformation)) 
    {
      $right = "User";
      switch ($row["rights"]) 
      {
        case 1:
          $right = "Administrator";
          break;
        case 2:
          $right = "Root Administrator";
      }
      $user=  array($row["ID"],$row["name"],$right,$row["email"]);
      array_push($userList, $user);
    }
    return $userList;
  }
  
  public function GetUser($ID) 
  {
    $queries = new mysqlqueries();
    
    $userInformation=$this->Connection->query($queries->GetIndex($queries->User,$ID, NULL,NULL));
    $user = NULL;
    while ($row = mysqli_fetch_array($userInformation)) 
    {
      $right = "User";
      switch ($row["rights"]) 
      {
        case 1:
          $right = "Administrator";
          break;
        case 2:
          $right = "Root Administrator";
      }
      $user=  array($row["ID"],$row["name"],$right,$row["email"],$row["password"]);
    }
    return $user;
  }
  
  public function IsAdmin($id,$userID) {
    $queries = new mysqlqueries();
    
    $userInformation=$this->Connection->query($queries->GetIndex($queries->User,$id, NULL,NULL));
    while ($row = mysqli_fetch_array($userInformation)) 
    {
      if($row["rights"]==1 || $row["rights"]==2){
        if($userID!=-1){
          if($this->IsRootAdmin($userID)){
            $this->AllowChangeRights = FALSE;
          }
          else {
            $this->AllowChangeRights=true;
          }
        }
        else{
          $this->AllowChangeRights = false;
      }
        return true;
      }
      else {
        $this->AllowChangeRights = false;
      return false;  
      }
    }
  }
  public function IsRootAdmin($id)
  {
    $queries = new mysqlqueries();
    
    $userInformation=$this->Connection->query($queries->GetIndex($queries->User,$id, NULL,NULL));
    while ($row = mysqli_fetch_array($userInformation)) 
    {
      if($row["rights"]==2){
        return true;
      }
      else {
      return false;  
      }
    }
    return false;
  }
  
  public function UpdateUser($column,$argument,$id) {
    $queries = new mysqlqueries();
    $this->Connection->query($queries->Update($queries->User, $argument, $id, $column,null));
  }
  
  public function AddUser($argument) {
   $queries = new mysqlqueries();
   $this->Connection->query($queries->Add($queries->User, $argument));
  }
  
  public function GetUserByName($name) {
    $queries=new mysqlqueries();
    $result = $this->Connection->query($queries->GetIndex($queries->User, $name, "name", null));
  
    $user = false;
    while ($row = mysqli_fetch_array($result)) 
    {      
      $user=  array($row["ID"],$row["name"],$row["rights"],$row["email"],$row["password"]);
    }
    return $user;
  }
  
  public function RemoveUser($id)
  {
    $queries = new mysqlqueries();
    $query = $queries->Remove($queries->User, NULL, $id);
    if(!$query)
    {
      return false;
    }
    $this->Connection->query($query);
    return true;
  }
  
  public $AllowChangeRights = false;
}
